﻿layui.config({
    base: "Scripts/ApplyMange/"
}).use(['form', 'layedit', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        laydate = layui.laydate;

    //常规用法   //文本框日期
    laydate.render({
        elem: '#test1'
    });

    laydate.render({
        elem: '#test2'
    });


    //添加按钮
    form.on('submit(btnAdd)', function (data) {
        //获取页面上文本框的值
        var name = $('#userName').val();
        var type = $('#leaveType option:selected').val();
        var leaveTime = $('#test1').val();
        var endTime = $('#test2').val();
        var reason = $('#reason').val();
        var remark = $('#remark').val();

        $.ajax({
            type: "POST",
            url: "/ApplyMange/Create",        //发送请求  
            data: {
                "Name": name,
                "TypeID": type,
                "LeaveTime": leaveTime,
                "EndTime": endTime,
                "LeaveReason": reason,
                "Remark":remark
            },
            dataType: "json",
            success: function (result) {
                layer.msg(result.Message, { icon: 16, time: false, shade: 0.8 });
                window.location.href = "/ApplyMange/Index";
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert("不存在该用户名！");
            }
        });
    });

    //请假类型下拉框
    function TypeSelect() {
        var url = "/ApplyMange/DropType";
        $.post(url, function (result) {
            var types = JSON.parse(result.data);
            var typesDrop = $("#leaveType");
            layui.each(types, function (index, data) {
                var typesOption = $("<option value='" + data.TypeID + "'>" + data.TypeName + "</option>");
                typesDrop.append(typesOption);
            });
            form.render();
        });
    };

    $(function () {
        TypeSelect();
    })


});